package com.example.myapplication1.dinybehaivor;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.ViewConfiguration;
import android.widget.FrameLayout;
import android.widget.OverScroller;
import android.widget.Scroller;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.ViewCompat;
import androidx.core.widget.NestedScrollView;

import com.example.myapplication1.R;

/**
 * Copyright (C), 2020, 重庆八进制有限公司
 *
 * @author: weng
 * @last-modifier: weng
 * @version: 1.0
 * @create-date: 2020/12/28 11:32
 * @last-modify-date: 2020/12/28 11:32
 * @description:
 */
public class MyFramelayout extends FrameLayout {

    private float mInitY;
    private float mLastMotionY;
    private VelocityTracker mVelocityTracker;
    private int mMinimumVelocity;
    private int mMaximumVelocity;
    private Scroller mScroller;

    public MyFramelayout(@NonNull Context context) {
        super(context);
        init(context);
    }

    public MyFramelayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MyFramelayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        init(context);
    }

    private void init(Context context) {
        final ViewConfiguration configuration = ViewConfiguration.get(getContext());
        mScroller = new Scroller(getContext());
        mMinimumVelocity = configuration.getScaledMinimumFlingVelocity();
        mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();

    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return true;
    }


    @Override
    public void computeScroll() {
        super.computeScroll();
        if (mScroller.computeScrollOffset()) {
            final int y = mScroller.getCurrY();
            Log.e("tag", "y----->" + y);
//            NestedScrollView nestedScrollView = findViewById(R.id.nestedview);
//            nestedScrollView.setY(-y);
            invalidate();
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
//        NestedScrollView nestedScrollView = findViewById(R.id.nestedview);

        initVelocityTrackerIfNotExists();
        MotionEvent vtev = MotionEvent.obtain(event);
        switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:
                mInitY = event.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                mLastMotionY = event.getRawY();
                float detalY = mLastMotionY - mInitY;
                mInitY = mLastMotionY;
//                nestedScrollView.setY(nestedScrollView.getY() + detalY);
                break;
            case MotionEvent.ACTION_UP:

                final VelocityTracker velocityTracker = mVelocityTracker;
                velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
                int initialVelocity = (int) velocityTracker.getYVelocity();
                if ((Math.abs(initialVelocity) >= mMinimumVelocity)) {

                    fling(-initialVelocity);
                }
                break;

        }
        if (mVelocityTracker != null) {
            mVelocityTracker.addMovement(event);
        }
        vtev.recycle();
        return true;
    }

    private void fling(int velocityY) {
        int x = (int) getX();
        int y = (int) getY();

        mScroller.fling(x, y, // start
                0, velocityY, // velocities
                0, 0, // x
                Integer.MIN_VALUE, Integer.MAX_VALUE); // overscroll
        invalidate();
    }


    private void initVelocityTrackerIfNotExists() {
        if (mVelocityTracker == null) {
            mVelocityTracker = VelocityTracker.obtain();
        }
    }
}
